
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    imgList: [],
    base64:"",
    imgStr: "",
    typeArr: ["便民服务", "二手闲置", "求职招聘", "买房租房", "奇趣杂谈", ],
    index: 0,
    // 
    title:"",
    phone:"",
    content:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    // this.GetStateBarTypeList();
  },

  // delTap
  delTap: function(e) {
    var imgList = this.data.imgList;
    imgList.splice(e.currentTarget.dataset.index, 1);
    this.setData({
      imgList: imgList,
    })
  },
  // chooseImg
  chooseImg: function(e) {
    var that = this;
    var base64 = this.data.base64;
    var imgSrc = this.data.imgSrc;
    var imgUrl = this.data.imgUrl;
    var imgList = this.data.imgList;
    wx.chooseImage({
      // 最多可以选择的图片张数，默认9
      count: 1,
      // 可以指定是原图还是压缩图，默认二者都有
      sizeType: ['original', 'compressed'],
      // 可以指定来源是相册还是相机，默认二者都有
      sourceType: ['album', 'camera'],
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths[0];
        wx.request({
          url: tempFilePaths,
          method: 'GET',
          responseType: 'arraybuffer',
          success: function(res) {
            base64 = wx.arrayBufferToBase64(res.data);
            that.setData({
              base64: base64,
              imgSrc: 'data:image/jpg;base64,' + base64
            })
            // console.log(base64)
            wx.request({
              url: baseJs.api + baseJs.encryUrl("method=LoadPic"),
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              method: 'post',
              data: baseJs.json2Form({
                HeadImage: base64,
              }),
              success: function(res) {
                console.log(res);
                imgList.push(res.data.ResultData);
                that.setData({
                  imgList: imgList,
                })
              },
              fail: function (res) {
                console.log(res);
                
              },
            })
          }
        })
      }
    })
  },
  // 帖子类型
  // GetStateBarTypeList: function () {
  //   var that = this;
  //   var uid = this.data.uid;
  //   var typeArr = this.data.typeArr;
  //   wx.request({
  //     url: baseJs.api + baseJs.encryUrl("method=GetStateBarTypeList"),
  //     success: function (res) {
  //       typeArr = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
  //       console.log(typeArr);
  //       that.setData({
  //         typeArr: typeArr,
  //       })
  //     }
  //   })
  // },
  bindPickerChange: function(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },

  titleInput:function(e){
    this.setData({
      title: e.detail.value
    })
  },

  phoneInput: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  // 
  contentInput: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  // 
  faBiaoTap: function() {
    var that = this;
    var uid = this.data.uid;
    var imgList = this.data.imgList;
    var imgStr = imgList.join(';');
    var index = this.data.index;
    var id = id + 1;
    var title = this.title;
    var content = this.data.content;
    var phone = this.data.phone;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=PublishBar"),
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'post',
      data: baseJs.json2Form({
        UID: uid,
        Title: title,
        Content: content,
        Images: imgStr,
        BarType: id,
        Phone: phone,
      }),
      success: function (res) {
        console.log(res);
        if (res.data.ResultFlag == 1) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({
              delta:1
            })
          }, 1000)
         
        } else {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
  },
})